package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetMealDishesMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealServer;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetMealServerImpl implements SetMealServer {
    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetMealDishesMapper setMealDishesMapper;

    @Override
    public PageResult select(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> pg = setMealMapper.select(setmealPageQueryDTO);
        return new PageResult( pg.getTotal(), pg.getResult());
    }

    @Override
    public void addSetMealAndDishes(SetmealDTO setmealDTO){
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.add(setmeal);
        Long MealId = setMealMapper.getIdByName(setmeal.getName());

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDishes1 -> {
            setmealDishes1.setSetmealId(MealId);
        });
        setMealDishesMapper.add(setmealDishes);
    }

    public SetmealVO getMealById(Long id){
        SetmealVO setmealVO = setMealMapper.selectById(id);
        List<SetmealDish> setmealDishes = setMealDishesMapper.getDishesBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    public void changeStatus(Long id, Integer status){
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setMealMapper.update(setmeal);
    }

    public void updateMeal(SetmealDTO setmealDTO){
        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO,setmeal);
        System.out.println(setmealDTO);
        setMealMapper.update(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        setMealDishesMapper.delete(setmealDTO.getId());

        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setMealDishesMapper.add(setmealDishes);
    }

    public void delete(List<Long> ids){
        setMealMapper.delete(ids);
        ids.forEach(id -> {setMealDishesMapper.delete(id);});
    }

    /**
     * 条件查询
     */
    public List<Setmeal> list(Setmeal setmeal) {
        return setMealMapper.list(setmeal);
    }

    /**
     * 根据id查询菜品选项
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}
